<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('所属组织')" prop="organisation">
            <DeptSelect v-model="form.organisation" :placeholder="$t('所属组织')" @input="getProgramCode" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资年度')" prop="annualInvestment">
            <el-input v-model="form.annualInvestment" :placeholder="$t('投资年度')" oninput="value=value.replace(/^\D*([0-9]\d*)?.*$/,'$1')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('编制人员')" prop="principalId">
            <UserSelect v-model="form.principalId" :placeholder="$t('编制人员')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('计划编码')" prop="programCode">
            <el-input v-model="form.programCode" :placeholder="$t('计划编码')" show-word-limit disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('维护时间')" prop="servicingTime">
            <el-date-picker
              v-model="form.servicingTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('维护时间')"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('币种')" prop="currency">
            <dict-select
              v-model="form.currency"
              dictionary="currency"
              :placeholder="$t('币种')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本年计划投资总额') + $t('金额单位')" prop="yearGrossAssets">
            <Money v-model="form.yearGrossAssets" :placeholder="$t('本年计划投资总额') + $t('金额单位')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系电话')" prop="phone">
            <el-input v-model="form.phone" :placeholder="$t('联系电话')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('财务性投资占比目标值(%)')" prop="investmentProportion">
            <Ratio v-model="form.investmentProportion" :placeholder="$t('财务性投资占比目标值(%)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('非主业投资占比目标值(%)')" prop="noPrincipalProportion">
            <Ratio v-model="form.noPrincipalProportion" :placeholder="$t('非主业投资占比目标值(%)')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="comment">
            <el-input v-model="form.comment" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <br>
      <br>
      <div class="area-header">
        <span class="area-header-title">{{ $t('年度计划明细') }}</span>
      </div>
      <el-table
        :data="form.investPlanAnnualPlanList"
        border
        show-summary
        :summary-method="getYearGrossAssets"
        class="nowrapTable formTable "
        style="overflow: auto;"
      >
        <el-table-column :label="$t('序号')" type="index" align="center" width="50" min-width="100">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('项目名称')" align="center" min-width="100">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('项目名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.dealName' " :rules="annualPlanRules.dealName">
              <el-input v-model="scope.row.dealName" :placeholder="$t('项目名称')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('项目类型')" align="center" min-width="100">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('项目类型') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.annualDealType' " :rules="annualPlanRules.annualDealType">
              <dictSelect
                v-model="scope.row.annualDealType"
                :clearable="true"
                dictionary="annual_deal_type"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('出资主体类型')" align="center" min-width="100">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('出资主体类型') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.contributiveType' " :rules="annualPlanRules.contributiveType">
              <dictSelect
                v-model="scope.row.contributiveType"
                :clearable="true"
                dictionary="contract_subject_type"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('投资主体')" align="center" min-width="150">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('投资主体') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.investor' " :rules="annualPlanRules.investor">
              <MainBodySelect
                v-model="scope.row.investor"
                :parameters="scope.row.contributiveType"
                :clearable="true"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('项目总投资额') + $t('金额单位')" align="center" min-width="100">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('项目总投资额') + $t('金额单位') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.totalProjectInvestment' " :rules="annualPlanRules.totalProjectInvestment">
              <Money v-model="scope.row.totalProjectInvestment" :placeholder="$t('项目总投资额') + $t('金额单位')" unit="w" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('本年计划投资') + $t('金额单位')" align="center" min-width="100">
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.yearInvestment' ">
              <Money v-model="scope.row.yearInvestment" :placeholder="$t('本年计划投资') + $t('金额单位')" unit="w" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('是否为重大战略任务')" align="center" min-width="100">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('是否为重大战略任务') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.isGreat' " :rules="annualPlanRules.isGreat">
              <dictSelect
                v-model="scope.row.isGreat"
                :clearable="true"
                dictionary="sys_yes_no"
              />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center" min-width="150">
          <template slot-scope="scope">
            <el-form-item :prop="'investPlanAnnualPlanList.' + scope.$index + '.comment' ">
              <el-input v-model="scope.row.comment" type="textarea" class="fileTextarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" min-width="60">
          <template slot-scope="scope">
            <el-button size="small" type="text" icon="el-icon-delete" @click="delHandle(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addAnnualPlan">{{ $t("添加") }}</el-button>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="investPlan:submission" />
    </div>
  </div>
</template>

<script>
    import { saveInvestPlanSubmission, getInvestPlanSubmission, gainProgramCode } from '@/api/fund/investPlan/investPlanSubmission'
    import { delInvestPlanAnnualPlan } from '@/api/fund/investPlan/investPlanAnnualPlan'
    import { getUser } from '@/api/system/user'
    import MainBodySelect from '@/components/MainBodySelect/index'
    export default {
      components: {
        MainBodySelect
      },
        data() {
            return {
                // 表单参数
                form: {
                  // 年度计划明细
                  investPlanAnnualPlanList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                organisation: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                programCode: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                annualInvestment: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                servicingTime: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                principalId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                currency: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                yearGrossAssets: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                  phone: [
                    {
                      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                      message: this.$t('格式错误'),
                      trigger: 'blur'
                    }
                  ]
                },
              annualPlanRules: {
                isGreat: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                totalProjectInvestment: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                investor: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                contributiveType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                annualDealType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                dealName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
              }
            }
        },

      watch: {
        'form.annualInvestment': {
          handler() {
            if (this.form.annualInvestment && this.form.organisation) {
              gainProgramCode(this.form.organisation).then(response => {
               var code = response.msg
                this.form.programCode = 'TZJH-' + code + '-' + this.form.annualInvestment + '001'
              })
            }
          }
        }
      },
        created() {
            this.getDicts('currency').then(response => {
                this.currencyOptions = response.data
            })
        },
        methods: {
          getProgramCode() {
            if (this.form.annualInvestment && this.form.organisation) {
              gainProgramCode(this.form.organisation).then(response => {
                var code = response.msg
                this.form.programCode = 'TZJH-' + code + '-' + this.form.annualInvestment + '001'
              })
            }
          },
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getInvestPlanSubmission(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                  getUser(this.$store.state.user.userId).then(response => {
                    this.form.organisation = response.data.deptId
                  })
                }
                    this.loading = false
                },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    organisation: undefined,
                    programCode: undefined,
                    annualInvestment: undefined,
                    servicingTime: new Date(),
                    principalId: this.$store.state.user.userId,
                    currency: 'CNY',
                    yearGrossAssets: undefined,
                    phone: undefined,
                    investmentProportion: undefined,
                    noPrincipalProportion: undefined,
                    comment: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveInvestPlanSubmission(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          delHandle(index, row) {
            const id = row.id
            if (id !== undefined && id !== '' && id != null) {
              this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
              })
                .then(function() {
                  return delInvestPlanAnnualPlan(id)
                }).then(() => {
                this.form.investPlanAnnualPlanList.splice(index, 1)
                return this.msgSuccess(this.$t('删除成功'))
              })
                .catch(function() {})
            } else {
              this.form.investPlanAnnualPlanList.splice(index, 1)
            }
          },
          addAnnualPlan() {
            const row = {
              id: undefined,
              submissionId: undefined,
              annualDealType: undefined,
              contributiveType: undefined,
              investor: undefined,
              totalProjectInvestment: undefined,
              yearInvestment: undefined,
              isGreat: undefined,
              comment: undefined
            }
            if (!(this.form.investPlanAnnualPlanList instanceof Array)) {
              this.$set(this.form, 'investPlanAnnualPlanList', [])
            }
            this.$nextTick(() => {
              this.form.investPlanAnnualPlanList.push(row)
            })
          },
          // 获取本年计划投资总额
          getYearGrossAssets() {
            var YearGrossAssets = 0
            this.form.investPlanAnnualPlanList.forEach((item) => {
              if (item.yearInvestment) {
                YearGrossAssets += Number(item.yearInvestment)
              }
            })
            this.form.yearGrossAssets = YearGrossAssets
          }
    }
    }
</script>
